package com.huabo.audit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.enums.ProcessVariableEnum;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.enums.ProcessEnum;
import com.huabo.audit.oracle.entity.AgentEntity;
import com.huabo.audit.oracle.entity.TblAttachment;
import com.huabo.audit.oracle.entity.TblAuditOption;
import com.huabo.audit.oracle.entity.TblCirculation;
import com.huabo.audit.oracle.entity.TblNbsjProject;
import com.huabo.audit.oracle.entity.TblNbsjQuestionEntity;
import com.huabo.audit.oracle.entity.TblNbsjSheetEntity;
import com.huabo.audit.oracle.entity.TblNbsjSheetReportEntity;
import com.huabo.audit.oracle.entity.TblOrganization;
import com.huabo.audit.oracle.mapper.TblAttachmentMapper;
import com.huabo.audit.oracle.mapper.TblCirculationMapper;
import com.huabo.audit.oracle.mapper.TblNbkzRiskMapper;
import com.huabo.audit.oracle.mapper.TblNbsjProjectMapper;
import com.huabo.audit.oracle.mapper.TblNbsjQuestionMapper;
import com.huabo.audit.oracle.mapper.TblNbsjSheetMapper;
import com.huabo.audit.oracle.vo.TBlNbsjSheetVo;
import com.huabo.audit.service.ActivityPluginsService;
import com.huabo.audit.service.ProcessService;
import com.huabo.audit.service.TblAuditOptionService;
import com.huabo.audit.service.TblNbsjProjectService;
import com.huabo.audit.service.TblNbsjSheetService;
import com.huabo.audit.util.PageInfo;
import com.huabo.audit.util.R;

import cn.hutool.core.util.StrUtil;

/**
* 描述:实现类
* @author: ziyao
* @date: 2022-04-20
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TblNbsjSheetServiceImpl extends ServiceImpl<TblNbsjSheetMapper, TblNbsjSheetEntity> implements TblNbsjSheetService {
    @Autowired
    private TblNbsjSheetMapper tblNbsjSheetMapper;
    
    @Resource
	private TblAttachmentMapper tblAttachmentMapper;
    
    @Autowired
    private ActivityPluginsService activityPluginsService;
    @Resource
   	public TblCirculationMapper tblCirculationMapper;
    @Resource
	private RuntimeService runtimeService;
	@Resource 
	private TaskService taskService;
	
    @Resource
    private ProcessService processService;
    
    @Resource
    private TblAuditOptionService tblAuditOptionService;
    
    @Resource
    private TblNbkzRiskMapper tblNbkzRiskMapper;
    
    @Resource
    private TblNbsjProjectService tblNbsjProjectService;
    
    @Resource
    private TblNbsjQuestionMapper tblNbsjQuestionMapper;
    
    @Resource
    private TaskRuntime taskruntime;
	@Resource
	private TblNbsjProjectMapper tblNbsjProjectMapper;
    
    
	@Override
	public JsonBean projectStandardDgPageList(String token, Integer pageNumber, Integer pageSize,TBlNbsjSheetVo tBlNbsjSheetVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	if(null == tBlNbsjSheetVo.getProjectid() ){
    		//==查询当前实施的项目！
    		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
    		if(tnp == null) {
    			return ResponseFormat.retParam(0,30003,resultMap);
    		}
    		Integer projectId = tnp.getProjectId();
    		if(null == projectId) {
    			return ResponseFormat.retParam(0,30003,resultMap);
    		}
    		tBlNbsjSheetVo.setProjectid(projectId);
    	}
    	
    	
    	PageInfo<TblNbsjSheetEntity> pageInfo = new PageInfo<TblNbsjSheetEntity>();
//    	tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjSheet);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
		TblNbsjProject project = tblNbsjProjectMapper.selectPJById(tBlNbsjSheetVo.getProjectid());
		resultMap.put("project", project);
    	pageInfo.setTlist(this.tblNbsjSheetMapper.selectListByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.setTotalRecord(this.tblNbsjSheetMapper.selectCountByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean projectStandardDgAdd(TblNbsjSheetEntity sheet, String token,String attids,String srJson) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Integer count = this.tblNbsjSheetMapper.selectPlanCodeByOrgid(sheet);
		if(count > 0) {
			return ResponseFormat.retParam(0,202,null);
		}
		
		//==查询当前实施的项目！
		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp == null) {
			return ResponseFormat.retParam(0,30003,null);
		}
		Integer projectId = tnp.getProjectId();
		if(null == projectId) {
			return ResponseFormat.retParam(0,30003,null);
		}
		sheet.setProjectId(projectId);
    	
		sheet.setAuditStaffId(loginStaff.getStaffid().intValue());
		sheet.setCreateTime(new Date());
		//根据planId主键是否为空判断新增还是修改 ，主键为空新增、不为空修改；
		
		if(sheet.getSheetId() != null) {
			//修改；
			this.tblNbsjSheetMapper.updateEntity(sheet);
			//==附件，先删除 再重新添加
			this.tblAttachmentMapper.deleteAttmentRelationSheet(sheet.getSheetId());
			if (attids != null && !"".equals(attids)) {
				String[] ids = attids.split(",");
				for (String id : ids) {
					this.tblAttachmentMapper.insertAttmentRelationSheet(id, sheet.getSheetId());
				}
			}
			
			//先清除 报告内容/问题描述
			this.tblNbsjSheetMapper.deleteSheetReportBySheetId(sheet.getSheetId());
			
		}else {
			//新增；
			sheet.setStatus(0);
			sheet.setState(TblNbsjSheetEntity.STATE1);
			this.tblNbsjSheetMapper.insertEntity(sheet);
			//==附件
			if (attids != null && !"".equals(attids)) {
				String[] ids = attids.split(",");
				for (String id : ids) {
					this.tblAttachmentMapper.insertAttmentRelationSheet(id, sheet.getSheetId());
				}
			}
		}
		
		//====报告内容/问题描述===========
		if(null != srJson && !"".equals(srJson.trim())) {
			List<TblNbsjSheetReportEntity> srList = new ArrayList<TblNbsjSheetReportEntity>();
			srList = JSONObject.parseArray(srJson,TblNbsjSheetReportEntity.class);
			for (TblNbsjSheetReportEntity sr : srList) {
				sr.setSheetId(sheet.getSheetId());
				this.tblNbsjSheetMapper.insertSheetReport(sr);
			}
		}
		
		//==审计发现
		String riskLevel = sheet.getRiskLevel();
		if("是".equals(riskLevel)) {
			TblNbsjQuestionEntity question = new TblNbsjQuestionEntity();
			question.setNbsjSheet(sheet);
			question.setStatus(TblNbsjQuestionEntity.NUSTATUS);
			question.setGroupStatus(TblNbsjQuestionEntity.GROUPNO);
//			this.tblNbsjQuestionService.save(question);
			this.tblNbsjQuestionMapper.insertEntity(question);
		}
		
		
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		resultMap.put("Sheet",sheet);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean projectStandardDgDelete(Integer sheetid, String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		TblNbsjSheetEntity plan = this.tblNbsjSheetMapper.selectById(sheetid);
		
		if(plan == null) {
			return ResponseFormat.retParam(0,50001,null);
		}
		
//		if (plan.getOpinionstatus().equals(TblNbsjAuditplan.SPNO)) {
//			this.tblNbsjSheetMapper.deleteAuditPlanEntityById(planId);
//			return ResponseFormat.retParam(0,200,null);
//        } else {
//            return ResponseFormat.retParam(0,50001,null);
//        }
		this.tblNbsjSheetMapper.deleteById(sheetid);
		return ResponseFormat.retParam(1,200,null);
	}

	@Override
	public JsonBean dgAllPageList(String token, Integer pageNumber, Integer pageSize,TBlNbsjSheetVo tBlNbsjSheetVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblNbsjSheetEntity> pageInfo = new PageInfo<TblNbsjSheetEntity>();
//    	tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjSheet);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjSheetMapper.selectListByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.setTotalRecord(this.tblNbsjSheetMapper.selectCountByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}
	
	@Override
	public JsonBean dgglPageList(String token, Integer pageNumber, Integer pageSize,TBlNbsjSheetVo tBlNbsjSheetVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	//==查询当前实施的项目！
		TblNbsjProject tnp = this.tblNbsjProjectService.getCurrenNbsjProjectByLoginStaff(loginStaff.getStaffid());
		if(tnp == null) {
			return ResponseFormat.retParam(0,30003,resultMap);
		}
		Integer projectId = tnp.getProjectId();
		if(null == projectId) {
			return ResponseFormat.retParam(0,30003,resultMap);
		}
		tBlNbsjSheetVo.setProjectid(projectId);
		TblNbsjProject project = tblNbsjProjectMapper.selectPJById(projectId);
		resultMap.put("project", project);
    	PageInfo<TblNbsjSheetEntity> pageInfo = new PageInfo<TblNbsjSheetEntity>();
//    	tblNbsjSheet.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjSheet);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblNbsjSheetMapper.selectListByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.setTotalRecord(this.tblNbsjSheetMapper.selectCountByPageInfo(pageInfo,tBlNbsjSheetVo));
    	pageInfo.getTotalPage();
    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}
	
	@Override
	public JsonBean findNbsjSheetDetail(String token, Integer sheetid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		TblNbsjSheetEntity plan = this.tblNbsjSheetMapper.selectById(sheetid);
		resultMap.put("sheet", plan);
		TblNbsjProject project = tblNbsjProjectMapper.selectPJById(plan.getProjectId());
		resultMap.put("project", project);
		
		//==
		List<TblNbsjSheetReportEntity> listSP = this.tblNbsjSheetMapper.selectListSheetReport(sheetid);
		if (null != listSP) {
			for (TblNbsjSheetReportEntity sp : listSP) {
				String orgids = sp.getSjdeptIds();
				
				String sysOrgName = "";
				List<TblOrganization> listOrg = this.tblNbkzRiskMapper.selectListOrgInId(orgids);
				if(null != listOrg) {
					for (int i = 0; i < listOrg.size(); i++) {
						TblOrganization org = listOrg.get(i);
						String orgname = org.getOrgname();
						sysOrgName += orgname;
						if((listOrg.size()-1) > i) {
							sysOrgName += ",";
						}
					}
				}
				sp.setSjdeptNames(sysOrgName);
			}
		}
		resultMap.put("listSP", listSP);
		
		return ResponseFormat.retParam(1,200,resultMap);
	}



	//    /**
//    * 条件查询 封装QueryWrapper
//    * @param model
//    * @return
//    */
//    @Override
//    public LambdaQueryWrapper<TblNbsjSheetEntity> onSelectWhere(TblNbsjSheetEntity model) {
//        LambdaQueryWrapper<TblNbsjSheetEntity>  queryWrapper=new QueryWrapper<TblNbsjSheetEntity>().lambda();
//        if (model == null) {
//            return queryWrapper;
//        }
//
//
//        return queryWrapper;
//    }
//
//    /**
//    *  封装保存方法
//    * @param model
//    * @return
//    */
//    @Override
//    public boolean saveTblNbsjSheet(TblNbsjSheetEntity model) {
//        //在保存和更新之前的操作 同步字段id、name
//        beforSaveandUpdate(model);
//
//        return save(model);
//    }
//
//    /**
//    *  封装更新方法
//    * @param model
//    * @return
//    */
//    @Override
//    public boolean updateTblNbsjSheet(TblNbsjSheetEntity model) {
//        if(model.getSheetId()==null || model.getSheetId()==0){
//            throw new CommercialException(ResultCode.BIZ_ERROR,"teamId不能为空！");
//        }
//        //在保存和更新之前的操作 同步字段id、name
//        beforSaveandUpdate(model);
//
//        return updateById(model);
//    }
//
    @Override
    public List<TblNbsjSheetEntity> selectByProjectId(Integer projectId) {

        return null;
    }
//
//    /**
//    * @description: 在保存和更新之前的操作 同步字段id、name
//    * @param model:
//    * @return: void
//    * @author: ziyao
//    * @date: 2022-04-20
//    */
//    private void beforSaveandUpdate(TblNbsjSheetEntity model){
//
//    }
//
//
//	@Override
//	public List<Object[]> OBJfindAllByProjectid(String string, BigDecimal staffid) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public void merge(TblNbsjSheetEntity blNbsjSheet) {
//		// TODO Auto-generated method stub
//		
//	}
//
//	@Override
//	public boolean getSheetByCode(Integer projectId, String sheetCode) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//
//	@Override
//	public Integer findByCount(String targetId) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public List<TblNbsjSheetEntity> findDgByQues(Integer projectId) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public List<TblNbsjSheetEntity> findDgByProjectId(Integer projectId, Object object) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	@Override
	public JsonBean submitArrpoval(String token, Integer sheetid) throws Exception {
		TblStaffUtil user = DealUserToken.parseUserToken(token);
		if(user == null) {
			return ResponseFormat.retParam(1,20006,null);
		}
    	
		TblNbsjSheetEntity sheet = tblNbsjSheetMapper.getById(sheetid.toString());
		if(sheet == null) {
			return ResponseFormat.retParam(1,50002,null);
		}
        TblCirculation cir = new TblCirculation();
        try {
           
           cir.setCytype(TblCirculation.TYPE_DGFH);
           cir.setCycode(sheet.getSheetCode());
           cir.setCyname(sheet.getSheetName());
           cir.setCydate(new Date());
           cir.setCystate(TblCirculation.STATE_FQ);
           cir.setCyurl(TblCirculation.URL_DGFH+sheetid);
           cir.setCyStaffid(user.getStaffid().toString());
           this.tblCirculationMapper.saveTblCirculation(cir);
           
           Map<String, Object> variables= new HashMap<String, Object>();
           variables.put(ProcessVariableEnum.model.toString(), cir);
           
  			AgentEntity ae = new AgentEntity();
  			if(sheet.getFirststaffid()==null||sheet.getSecondstaffid()==null) {
  				return ResponseFormat.retParam(0,"请添加复核人",null);
  			}
  			ae.setReviewer(sheet.getFirststaffid().toString());
  			ae.setReviewer_2(sheet.getSecondstaffid().toString());
		   variables.put(ProcessVariableEnum.agent.toString(), ae);
           
           ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessEnum.SJ_DGFH.name(),cir.getCyid().toString(), variables);
           Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
           cir.setTaskid(task.getId());
           //流程定义Id
           String businessKey = processInstance.getBusinessKey();
           String definitionId = processInstance.getProcessDefinitionId();
           cir.setBusinesskey(businessKey);
           cir.setDefinitionid(definitionId);
           this.tblCirculationMapper.updateCirculationInfoById(cir);
           taskService.complete(task.getId());
           sheet.setState(TblNbsjSheetEntity.STATE2);
           tblNbsjSheetMapper.updateEntity(sheet);
        } catch (Exception e) {
        	e.printStackTrace();
        	if(cir != null && cir.getCyid() != null) {
        		this.tblCirculationMapper.deleteEntityById(cir.getCyid());
        	}
        	return ResponseFormat.retParam(1,30002,null);
        }	
        return ResponseFormat.retParam(1,200,null);
	}

	@Override
	public JsonBean getApprovalInfo(String token, Integer sheetid, String taskId, Integer cyId) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return ResponseFormat.retParam(1,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblNbsjSheetEntity sheet = this.tblNbsjSheetMapper.selectById(sheetid);//tblNbsjSheetMapper.getById(sheetid.toString());
		
		if(sheet == null) {
			return ResponseFormat.retParam(1,50002,null);
		}
		
		//==
		List<TblNbsjSheetReportEntity> listSP = this.tblNbsjSheetMapper.selectListSheetReport(sheetid);
		if (null != listSP) {
			for (TblNbsjSheetReportEntity sp : listSP) {
				String orgids = sp.getSjdeptIds();
				String sysOrgName = "";
				List<TblOrganization> listOrg = this.tblNbkzRiskMapper.selectListOrgInId(orgids);
				if(null != listOrg) {
					for (int i = 0; i < listOrg.size(); i++) {
						TblOrganization org = listOrg.get(i);
						String orgname = org.getOrgname();
						sysOrgName += orgname;
						if((listOrg.size()-1) > i) {
							sysOrgName += ",";
						}
					}
				}
				sp.setSjdeptNames(sysOrgName);
			}
		}
		resultMap.put("listSP", listSP);
		
        List<String> btnList = null;
    	if(sheet!=null){
    		//审批按钮
    		if(StrUtil.isNotBlank(taskId) && sheet.getState()!= TblNbsjSheetEntity.STATE5){
    			btnList = processService.getButtonsForTransition(taskId);
    			resultMap.put("btnList", btnList);
    		}
    		//审批记录
    		List<TblAuditOption> ao = tblAuditOptionService.findOptionByRelationId(sheetid.toString(),cyId);
    		if (cyId != null){
    			TblCirculation cy = this.tblCirculationMapper.findById(cyId.toString());
    			resultMap.put("cy", cy);
    		}
    		
    		resultMap.put("taskId", taskId);
    		resultMap.put("aoptionList", ao);
    		resultMap.put("cyId", cyId);
    		resultMap.put("sheet", sheet);
    		TblNbsjProject project = tblNbsjProjectMapper.selectPJById(sheet.getProjectId());
    		resultMap.put("project", project);
    	}
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean dealApporval(String token, Integer cyId, String taskId, String transition, String optDesc,
			String sheetid) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return ResponseFormat.retParam(1,20006,null);
		}
		TblNbsjSheetEntity sheet = tblNbsjSheetMapper.getById(sheetid.toString());
		if(sheet == null) {
			return ResponseFormat.retParam(1,50002,null);
		}
		JsonBean jsonBean = null;
		TblCirculation cy = this.tblCirculationMapper.findById(cyId.toString());
		if (null != cy) {
			Map<String, Object> variables = new HashMap<>();
			variables.put(ProcessVariableEnum.model.toString(), cy);
			if (StrUtil.isNotBlank(transition)) {
				variables.put(ProcessVariableEnum.transition.toString(), transition);
			}
			try {
				taskService.claim(taskId, staff.getStaffid().toString());
				taskService.complete(taskId, variables);
				//taskruntime.claim(TaskPayloadBuilder.claim().withTaskId(taskId).withAssignee(staff.getStaffid().toString()).build());
				//taskruntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).withVariables(variables).build());
			} catch (Exception e) {
				e.printStackTrace();
				return ResponseFormat.retParam(1,30002,null);
			}
			//添加审批意见
			TblAuditOption opt = new TblAuditOption();
			opt.setCyid(cy.getCyid());
			opt.setOptStaffid(staff.getStaffid());
			opt.setStaffidName(staff.getRealname());
			opt.setOptDesc(optDesc);
			opt.setRelationId(new BigDecimal(sheetid.toString()));
			opt.setOptState(transition);
			jsonBean = this.tblAuditOptionService.saveAuditOptionInfo(cy,opt);
		}
		return jsonBean;
	}

	@Override
	public List<TblNbsjSheetEntity> OBJfindAllByProjectid(Integer projectid, String token) throws Exception {
		TblStaffUtil staff = DealUserToken.parseUserToken(token);
		if(staff == null) {
			return null;
		}
		
		List<TblNbsjSheetEntity> list = this.tblNbsjSheetMapper.getExportList(projectid);
		return list;
	}

	@Override
	public JsonBean sheetReportDel(String token, Integer reportid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		this.tblNbsjSheetMapper.deleteSheetReportById(reportid);
		
		return ResponseFormat.retParam(1,200,null);
	}
	
	
	
	
	@Override
	public R removeAttInfoByAttId(String token, String attId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return R.fail("用户已失效！");
		}
        return this.deleteRealtionAttInfo(attId);
	}

	private R deleteRealtionAttInfo(String attId) throws Exception {
		boolean b = false;
        TblAttachment att = this.tblAttachmentMapper.selectEntityById(attId);
        this.tblNbsjSheetMapper.deleteFileInfoByAttId(att.getAttid().intValue());
        this.tblAttachmentMapper.deleteEntity(att.getAttid());
        
        return R.success();
	}
}
